<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      #box {
        width: 100px;
        height: 100px;
        background-color: red;
      }
    </style>
  </head>
  <body>
    <div id="box"></div>
    <script>
      const oBox = document.getElementById("box");

      /* let lastTime = 0;
      oBox.onmousemove = function () {
        let nowTime = Date.now();
        //看门狗
        if (nowTime - lastTime <= 500) return;

        //一旦狗子放行,就要把lastTime变成nowTime的时间,供下一次使用
        lastTime = nowTime;

        console.log(1);
      }; */

      function center(e) {
        console.log("核心代码", this, e);
      }

      oBox.onmousemove = throttle(center, 500);

      //节流函数
      function throttle(fn, time) {
        //真正的事件函数,只要事件触发就会一直执行
        let lastTime = 0;
        return function (e) {
          let nowTime = Date.now();
          if (nowTime - lastTime <= time) return;
          lastTime = nowTime;

          center.call(this, e);
        };
      }
    </script>
  </body>
</html>
